﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Widget.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "This is our project!";

            return View();
        }

        public ActionResult About()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Services()
        {
            //Linq to SQL
            var db = new DataClassesWidgetDataContext();
            var serv = db.Services.Select(e => new
            {
                e.ServiceName,
                e.ServicePrice,
                e.ServiceDescription,
                e.ServiceGroup,
                e.ProviderID
            });
            return Json(serv, JsonRequestBehavior.AllowGet);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Resources(string serviceName)
        {
            var db = new DataClassesWidgetDataContext();
            var servId = db.Services.Where(e => e.ServiceName == serviceName
                ).Select(e => new
                {
                    e.ServiceID
                }).First().ServiceID;
            var resour = db.Resources.Where(e => e.ServiceID == servId).Select(e => new
            {
                e.ResourceName,
                e.ResourceDescription
            });

            return Json(resour, JsonRequestBehavior.AllowGet);

        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Providers(int providerId)
        {
            var db = new DataClassesWidgetDataContext();
            var prov = db.Providers.Where(e=>e.ProviderID == providerId).Select(e => new
            {
                e.ProviderName,
                e.ProviderAddress,
                e.ProviderPhone
            });
            return Json(prov, JsonRequestBehavior.AllowGet);
        }
    }
}
